<?php
//业主数据接口
namespace app\index\controller;
use think\Db;
class Ower extends Base{
	//获取小区数据
    public function plot_list(){
    	$param = $this->_data;
    	$page = !empty($param['page']) ? $param['page'] :1;
    	$map=[
    		'del' =>0,
    		'stat'=>1
    	];
    	if(!empty($param['tit'])){
    		$map['tit']=['like',"%".$tit."%"];
    	}
    	$rel=Db::name('plot')->field('id,tit,address')->where(['del'=>0,'stat'=>1])->page($page,10)->select();
    	if(!$rel){
    		return rel(-1,'没有更多数据');
    	}else{
    		return rel(1,'获取房源数据成功',$rel);
    	}
    }
    //业主获取自己的房源列表
    public function house_list(){
    	$user =$this->_user;
    	$param=$this->_data;
    	$page=!empty($param['page']) ? $param['page'] : 1;
    	$map['uid']=$user['id'];
    	$map['del']=0;
    	
    	if(!empty($param['tit'])) $map['tit']=['like','%'.$param['tit'].'%'];
    	if(!empty($param['selled'])) $map['selled']=$param['selled'];
    	
    	
    	$rel=Db::name('house')
    	->field('id as house_id,tit,address,price,decl,plot_tit,imgs,selled')
    	->where($map)
    	->page($page,10)->select();
    	if(!$rel){
    		return rel(-1,'没有更多数据');
    	}else{
    		return rel(1,'获取房源数据成功',$rel);
    	}
    }
	//业主上传及编辑房源
	public function edit_house(){
		$user	=$this->_user;
		$param	=$this->_data;
		$val	=validate('Ower');
		if(!$val->scene('put_house')->check($param)) return rel(-1,$val->getError());
		$plot=$val->plot;			//小区数据
		$param['plot_tit']=$plot['tit'];
		
		if(!empty($param['id'])){	//修改
			$house=Db::name('house')->where('id',$param['id'])->find();
			if($house['uid']!=$user['id']) return rel(-1,'您不是该房源房主');
			$rel=Db::name('house')->where('id',$param['id'])->update($param);
			if(!$rel){
				return rel(-1,'修改失败，稍后重试');
			}else{
				return rel(1,'修改成功');
			}
		}else{
			$param['uid']  =$user['id'];
			$param['phone']=$user['phone'];
			$param['lng']  =$plot['lng'];
			$param['lat']  =$plot['lat'];
			$rel=Db::name('house')->insert($param);
			if(!$rel){
				return rel(-1,'添加失败，稍后重试');
			}else{
				return rel(1,'添加成功');
			}
		}
	}
	//更改业主房源显示状态
	public function change_stat(){
		$user	=$this->_user;
		$param	=$this->_data;
		$val	=validate('Ower');
		if(!$val->scene('change_stat')->check($param)) return rel(-1,$val->getError());
		$house  =$val->house;
		if($house['uid']!=$user['id']) return rel(-1,'您不是该房源房主');
		
		if($house['stat']==0){
			$stat=1;
		}else{
			$stat=0;
		}
		$rel=Db::name('house')->where('id',$param['house_id'])->update(['stat'=>$stat]);
		if(!$rel){
			return rel(-1,'切换状态失败');
		}else{
			return rel(1,'切换状态成功',$stat);
		}
	}
	//通过租客手机号码，查询该租客信息
	public function tenant_by_phone(){
		$param=$this->_data;
		if(!isset($param['phone']) || empty($param['phone'])) return rel(-1,'租客手机号码不能为空');
		if(!check_mobile($param['phone'])) return rel(-1,'手机号码格式错误');
		$tenant=Db::name('users')->field('id as tenant_id,idcard,phone,name')->where(['phone'=>$param['phone'],'del'=>0])->find();
		if(!$tenant){
			return rel(-1,'该用户不存在');
		}else{
			return rel(1,'获取租客信息成功后',$tenant);
		}
	}
	//业主发起合同
	public function write_contract(){
		$user	=$this->_user;
		if(request()->isGet()){
			$ower=[
				'name'  =>$user['name'],
				'idcard'=>$user['idcard'],
				'phone' =>$user['phone']
			];
			return rel(1,'获取业主数据成功',$ower);
		}else{
			$param	=$this->_data;
			$val	=validate('Ower');
			if(!$val->scene('change_stat')->check($param)) return rel(-1,$val->getError());
			$house  =$val->house;
			$tenant =$val->user;
			if($house['selled']==2) return rel(-1,'该房源已出租');
			$insert=[
				'ower_id'	=>$user['id'],
				'tenant_id' =>$tenant['id'],
				'house_id'  =>$param['house_id'],
				'ding_price'=>!empty($param['ding_price']) ? $param['ding_price'] : 0,
				'price'		=>$param['price'],
				'stime' 	=>$param['stime'],
				'etime'		=>$param['etime'],
				'ctime'		=>time()
			];
			$rel=Db::name('contract')->insertGetId($insert);
			if(!$rel){
				return rel(-1,'发起合同失败，稍后重试');
			}else{
				$message=controller('common/Message','Logic');
    			$message->put_message(2,$rel,$tenant['id'],$user['id']);	//插入合同消息
    			return rel(-1,'发起合同成功');
			}
		}
	}
	//获取业主自己发起的合同数据
	public function contract(){
		$user=$this->_user;
		$param=$this->_data;
		$map['ower_id']=$user['id'];
		if(!empty($param['stat'])) $map['c.stat']=$param['stat'];
		$page = !empty($param['page']) ? $param['page'] : 1 ;
		
		$rel=Db::name('contract')->alias('c')
		->field('c.*,u.name,u.phone,u.idcard,h.tit as house_tit')
		->join('users u','c.tenant_id=u.id')
		->join('house h','c.house_id=h.id')
		->where($map)
		->page($page,10)
		->select();
		if(!$rel){
			return rel(-1,'没有更多数据');
		}else{
			return rel(1,'获取数据成功',$rel);
		}
	}
	//业主获取自己的租客
	public function my_tenant(){
		$user=$this->_user;
		$param=$this->_data;
		$page = !empty($param['page']) ? $param['page'] : 1;
		
		$map['ower_id']=$user['id'];
		
		if($param['type']==1){			//查询预约租客数据
			$map['t.stat']=1;//待处理的申请1：待处理，2：已确认
			$tab='seeHouse';
		}else{							//查询租客数据
			$map['t.stat']=2;//待处理的申请1：待处理，2：已确认，3：过期，4：作废
			$tab='contract';
		}
		$rel=Db::name($tab)->alias('t')
			->field('t.*,u.nickname,u.head_img,u.phone as tenant_phone')
			->join('users u','t.tenant_id=u.id')
			->where($map)
			->page($page,10)
			->select();
		
		if(!$rel){
			return rel(-1,'没有更多数据');
		}else{
			return rel(1,'获取数据成功',$rel);
		}
	}
	//业主通过租客申请的看房请求
	public function ok_see_house(){
		$user=$this->_user;
		$param=$this->_data;
		if(!isset($param['id']) || empty($param['id'])) return rel(-1,'申请主键不能为空');
		
		$see_house=Db::name('seeHouse')->where('id',$param['id'])->find();
		if(!$see_house) return rel(-1,'无效的申请主键');
		if($see_house['stat']==2) return rel(-1,'该申请已通过');
		
		$rel=Db::name('seeHouse')->where('id',$param['id'])->update(['stat'=>2]);
		if(!$rel){
			return rel(-1,'通过失败，稍后重试');
		}else{
			return rel(1,'通过成功');
		}
	}
	//业主发起生活账单通知
	public function bill(){
		$user=$this->_user;
		$param=$this->_data;
		
		$val=validate('Ower');
		if(!$val->scene('bill')->check($param)) return rel(-1,$val->getError());
		
		$contract=Db::name('contract')->where('id',$param['contract_id'])->find();
		if(!$contract || $contract['ower_id']!=$user['id']) return rel(-1,'无效的合同主键');
		if(!$contract['stat']!=2) return rel(-1,'该合同未签署');
		
		$message=Db::name('message')->where(['type'=>4,'target_id'=>$contract['tenant_id'],'source_id'=>$user['id']])->find();
		if($message && $message['stat']==1) return rel(-1,'您已经发过生活账单');
		
		$water_price=!empty($param['water_price']) ? $param['water_price'] : 0;	//水费
		$elect_price=!empty($param['elect_price']) ? $param['elect_price'] : 0; //电费
		$manag_price=!empty($param['manag_price']) ? $param['manag_price'] : 0; //管理费
		$total_price=$water_price + $elect_price + $manag_price;				//总费用
		
		$insert=[
			'ower_id'  =>$user['id'],
			'tenant_id'=>$contract['tenant_id'],
			'house_id' =>$contract['house_id'],
			'contract_id'=>$contract['id'],
			'total_price'=>$total_price,
			'water_price'=>$water_price,
			'elect_price'=>$elect_price,
			'manag_price'=>$manag_price,
			'ctime'      =>time()
		];
		$rel=Db::name('bill')->insertGetId($insert);
		if(!$rel){
			return rel(-1,'发布失败，稍后重试');
		}else{
			$messageObj=controller('common/Message','Logic');
			$messageObj->put_message(4,$rel,$$contract['tenant_id'],$user['id']);	//插入合同消息
			return rel(1,'发布成功');
		}
	}
}